from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render

from .models import VStuShow, UserInfo, JZG, YJS, Settings
from .utils import Department


# Create your views here.
class MotoModel:
    isshow = False
    moto = None
    moto_url = None

    def __init__(self, moto, moto_url):
        if moto != "":
            self.isshow = True
        self.moto_url = moto_url
        self.moto = moto


def index(request):
    context = dict()
    moto_value = Settings.objects.get(key="moto").value
    moto_url = Settings.objects.get(key="moto_url").value
    if moto_value != "":
        moto = MotoModel(moto_value, moto_url)
    else:
        moto = MotoModel("", "")
    context["moto"] = moto
    if request.method == 'GET':
        request.session.clear()
        return render(request, "index.html", context)
    else:
        username = request.POST.get("username", None)
        passwd = request.POST.get("passwd", "000000")[:6]
        if passwd is not None:
            passwd = str(passwd).replace("x", "X")
        usertype = int(request.POST.get("type", None))
        try:
            if usertype == 0:
                user = VStuShow.objects.db_manager("jw").get(username=username, idnumber__endswith=passwd)
                usr_show = UserModel(username=username, usertype=usertype)
                usr_show.realname = user.realname
                usr_show.gander = user.gendar
                usr_show.department = user.department
                usr_show.batch = user.class_field

            elif usertype == 1:
                user = YJS.objects.get(userid=username, idnumber__endswith=passwd)
                usr_show = UserModel(username=username, usertype=usertype)
                usr_show.realname = user.realname
                usr_show.gander = user.get_gander_display()
                usr_show.department = Department(user.depid).getroot().name
                usr_show.batch = usr_show.department
            elif usertype == 2:
                user = JZG.objects.get(userid=username, idnumber__endswith=passwd)
                usr_show = UserModel(username=username, usertype=usertype)
                usr_show.realname = user.realname
                usr_show.gander = user.get_gander_display()
                try:
                    usr_show.department = Department(user.depid).getroot().name
                except ObjectDoesNotExist:
                    usr_show.department = "Unknown"
                usr_show.batch = "教职工"
            try:
                userinfo = UserInfo.objects.get(userid=username, usertype=usertype)
                context["Complete"] = True
            except ObjectDoesNotExist as e:
                userinfo = DefaultUserinfo(username)
                context["Complete"] = False
            context["user"] = usr_show
            context["userinfo"] = userinfo
        except ObjectDoesNotExist as e:
            context["notValid"] = True
            return render(request, "index.html", context)
        request.session["user"] = usr_show.userid
        request.session["user_type"] = usertype
        context["user_type"] = usertype

        return render(request, "confirm.html", context)


class DefaultUserinfo:
    userid = None
    phonenumber = "未知"
    residence = "未知"
    isplanedtravel = "未知"
    isinschool = "未知"

    def __init__(self, username):
        self.userid = username


class UserModel:
    userid = None  # 学工号
    gander = None  # 性别
    realname = None  # 姓名
    usertype = None  # 类别
    department = None  # 学院
    duty = None  # 职务
    batch = None  # 班级

    def __init__(self, username, usertype):
        self.userid = username
        if usertype == 0:
            self.usertype = "本科生"
        elif usertype == 1:
            self.usertype = "研究生"
        elif usertype == 2:
            self.usertype = "教职工"
